<?php
use Workerman\Worker;
use Workerman\Connection;
use Workerman\Lib\Timer;
use Utils\Analyze;
use Utils\UserUtils;
use Utils\TalkUtils;
use Utils\RequstUtils;
use Utils\TipUtils;
use Utils\SignUtils;
require_once __DIR__ . '\Workerman\Autoloader.php';
require_once __DIR__ . '\Utils\Analyze.php';
require_once __DIR__ . '\Utils\UserUtils.php';
require_once __DIR__ . '\Utils\RequestUtils.php';
require_once __DIR__ . '\Utils\TipUtils.php';
require_once __DIR__ . '\Utils\SignUtils.php';
date_default_timezone_set('Asia/Shanghai');
// 创建一个Worker监听2345端口，使用http协议通讯
$http_worker = new Worker("tcp://0.0.0.0:2345");
// 启动4个进程对外提供服务
$http_worker->count = 4;
$http_worker->connections= array();
Analyze::$LOGIN= function ($d,$conn){
    global $http_worker;
    $res=UserUtils::login($d);
    $conn->send("login#".$res."\r\n");
    $res= (array)json_decode($res);
    if($res["code"]==200){
        $userinfo=(array)$res["userinfo"];
     $conn->id=   $userinfo["student_id"];
    $http_worker->connection[$userinfo["student_id"]]=$conn;
    }
};
Analyze::$TALKING=function($d,$conn){
    $conn->send("talking#".json_encode(TalkUtils::Create((array)json_decode($d)))."\r\n",true);
};
Analyze::$REQUEST=function($d,$conn){
    $conn->send("request#".json_encode(RequstUtils::Create((array)json_decode($d)))."\r\n",true);;
};
Analyze::$GET= function($d,$conn){
    $option=(Array)json_decode($d);
    $arr=array();
    switch ($option["option"]) {
        case 'fresh':
            $res= TalkUtils::fresh();
            if(count($res))
               $arr["chat"]=$res;
            $res2= Utils\RequstUtils::fresh();
            if(count($res2))
               $arr["request"]=$res2;
            $js=json_encode($arr);
            $conn->send("get#".$js."\r\n",true);
            break;
        case 'chat':
            $res= TalkUtils::loadmore($option["start"]);
            if(count($res))
               $arr["chat"]=$res;
            else
                $arr["chat"]=array();
            $js=json_encode($arr);
            $conn->send("get#".$js."\r\n",true);
            break;
        case 'chat':
            $res= RequestUtils::loadmore($option["start"]);
            if(count($res))
               $arr["request"]=$res;
            else
                $arr["chat"]=array();
            $js=json_encode($arr);
            echo $js;
            $conn->send("get#".$js."\r\n",true);
            break;
        default:
            # code...
            break;
    }
};
Analyze::$REGIST= function($d,$conn){
    $result= \Utils\UserUtils::Jion($d);
    if($result>0){
        $conn->send("regist#".json_encode(array("code"=>200))."\r\n");
    }
     else
        $conn->send("regist#".json_encode(array("code"=>400))."\r\n");
};
Analyze::$TIP= function ($d,$conn){
    $conn->send("tip#".json_encode(TipUtils::Add($d))."\r\n");
};
Analyze::$CHANGEPWD= function ($d,$conn){
    $conn->send("change#".json_encode(UserUtils::changepwd($d))."\r\n");
};
Analyze::$SIGNED=function ($d,$conn){
    $conn->send("signed#".json_encode(SignUtils::add($d))."\r\n");
};
// 接收到浏览器发送的数据时回复hello world给浏览器
$http_worker->onMessage = function($connection, $data)    
{ 
    $res=Analyze::deal($data,$connection);
};
$http_worker->onConnect= function ($connection){
    $connection->send("system#success\r\n");
};
$http_worker->onWorkerStart= function (){
     Timer::add(300, function (){
         TalkUtils::clear();
         RequstUtils::clear();
});
};
       
// 运行worker
Worker::runAll();